<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Kotlin基础语法, 安卓开发猿">
    <meta name="description" content="Android开发技术博客">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Kotlin基础语法 | 安卓开发猿</title>
    <link rel="icon" type="image/png" href="/blog/favicon.png">

    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>
    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="安卓开发猿" type="application/atom+xml">
</head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                    <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">安卓开发猿</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="https://xiayunquan.gitee.io/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/tools" class="waves-effect waves-light">
      
      <i class="fas fa-list" style="zoom: 0.6;"></i>
      
      <span>在线工具箱</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">安卓开发猿</div>
        <div class="logo-desc">
            
            Android开发技术博客
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="https://xiayunquan.gitee.io/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/tools" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-list"></i>
			
			Tools
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/44.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Kotlin基础语法</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/blog/tags/Kotlin%E8%AF%AD%E6%B3%95/">
                                <span class="chip bg-color">Kotlin语法</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/blog/categories/Kotlin/" class="post-category">
                                Kotlin
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-09-24
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-02-21
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    1.5k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    6 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h4 id="定义包"><a href="#定义包" class="headerlink" title="定义包"></a>定义包</h4><pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">package</span> my<span class="token punctuation">.</span>demo

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span><span class="token operator">*</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="定义函数"><a href="#定义函数" class="headerlink" title="定义函数"></a>定义函数</h4><p>带有两个 Int 参数、返回 Int 的函数：</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">sum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span> Int <span class="token punctuation">&#123;</span>
	<span class="token keyword">return</span> a <span class="token operator">+</span> b
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>将表达式作为函数体、返回值类型自动推断的函数</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">sum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">=</span> a <span class="token operator">+</span> b<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>函数返回无意义的值</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">printSum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span>Unit<span class="token punctuation">&#123;</span>
  	<span class="token comment">//$varName 表示变量值,$&#123;表达式&#125;执行表达式</span>
	<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"sum of <span class="token interpolation variable">$a</span> and <span class="token interpolation variable">$b</span> is <span class="token interpolation"><span class="token delimiter variable">$&#123;</span>a <span class="token operator">+</span> b<span class="token delimiter variable">&#125;</span></span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>Unit返回类型可以省略</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">printSum</span><span class="token punctuation">(</span>a<span class="token operator">:</span> Int<span class="token punctuation">,</span> b<span class="token operator">:</span> Int<span class="token punctuation">)</span><span class="token operator">:</span>Unit<span class="token punctuation">&#123;</span>
	<span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"sum of <span class="token interpolation variable">$a</span> and <span class="token interpolation variable">$b</span> is <span class="token interpolation"><span class="token delimiter variable">$&#123;</span>a <span class="token operator">+</span> b<span class="token delimiter variable">&#125;</span></span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>可变长参数函数,用 vararg 关键字进行标识：</p>
<p>函数的变长参数可以用 vararg 关键字进行标识：</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">vars</span><span class="token punctuation">(</span><span class="token keyword">vararg</span> v<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span>it <span class="token keyword">in</span> v<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    	<span class="token function">print</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>vars(1,2,3,4) ,输出1234</p>
<h4 id="定义变量"><a href="#定义变量" class="headerlink" title="定义变量"></a>定义变量</h4><p>定义只读局部变量使用关键字 <code>val</code> 定义。只能为其赋值一次。</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">val</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">1</span> <span class="token comment">//立即赋值</span>
<span class="token keyword">val</span> b <span class="token operator">=</span> <span class="token number">3.0</span> <span class="token comment">//自动推断类型为Short</span>
<span class="token keyword">val</span> c<span class="token operator">:</span> Double <span class="token comment">//变量申明</span>
c <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">.</span>9D <span class="token comment">//赋值</span>

<span class="token comment">// Byte 定义字节值</span>
<span class="token keyword">var</span> b<span class="token operator">:</span>byte <span class="token operator">=</span> <span class="token number">127</span>

<span class="token comment">// Short 定义短整型值</span>
<span class="token keyword">var</span> number<span class="token operator">:</span>Short <span class="token operator">=</span> <span class="token number">32767</span>

<span class="token comment">// Int 定义Int值</span>
<span class="token keyword">var</span> age<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">2017</span> 或者 <span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">2017</span> （自动推断类型）

<span class="token comment">// Long 定义长整型</span>
<span class="token keyword">var</span> money<span class="token operator">:</span> Long <span class="token operator">=</span> <span class="token number">9999999999L</span> 或者 <span class="token keyword">var</span> money <span class="token operator">=</span> <span class="token number">99999999999L</span> 

<span class="token comment">// Float 定义单精度值</span>
<span class="token keyword">var</span> f<span class="token operator">:</span> Float <span class="token operator">=</span> <span class="token number">123456789f</span> 或者 <span class="token keyword">var</span> f <span class="token operator">=</span> <span class="token number">123456789f</span>

<span class="token comment">// Double 定义双精度值</span>
<span class="token keyword">var</span> d<span class="token operator">:</span> Double <span class="token operator">=</span> <span class="token number">66666666.0</span> 或者 <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token number">66666666.0</span>

<span class="token comment">// boolean 定义布尔值</span>
<span class="token keyword">var</span> biu <span class="token operator">=</span> <span class="token boolean">true</span><span class="token operator">/</span><span class="token boolean">false</span>

<span class="token comment">// Char 定义字符</span>
<span class="token keyword">var</span> cili <span class="token operator">=</span> <span class="token string">"c"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>定义重复赋值变量使用<code>var</code> 关键字</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">var</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">32</span>
a <span class="token operator">=</span> <span class="token number">2</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h4 id="延迟初始化属性-（Late-Initialized-Properties）"><a href="#延迟初始化属性-（Late-Initialized-Properties）" class="headerlink" title="延迟初始化属性 （Late-Initialized Properties）"></a>延迟初始化属性 （Late-Initialized Properties）</h4><p>在Kotlin中，声明为具有非空类型的属性必须在构造函数中初始化，但是往往不希望在构造函数中初始化，例如在通过依赖注入或单元测试的设置方法来初始化属性的时候，不能在构造器中提供一个非空的初始化语句，为了处理这种情况，就要在属性上加lateinit关键字来延迟初始化</p>
<pre class="line-numbers language-none"><code class="language-none">public class MyTest &#123;
    lateinit var subject: TestSubject

    @Before fun setup() &#123;
        subject &#x3D; TestSubject()
    &#125;

    @Test fun test() &#123;
        subject.method() 
    &#125;
&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>注：<strong>lateinit</strong>只能够在var类型的属性中，不能用于构造函数，而且属性不能有自定义的getter和setting，这些属性必须是非空类型，并且不能是基本类型。 如果在一个延迟初始化的属性初始化前调用,会导致一个特定异常,调用的时候值还没有初始化.</p>
<h4 id="NULL检查机制"><a href="#NULL检查机制" class="headerlink" title="NULL检查机制"></a>NULL检查机制</h4><p>Kotlin的空安全设计对于声明可为空的参数，在使用时要进行空判断处理，有两种处理方式，字段后加!!像Java一样抛出空异常，另一种字段后加?可不做处理返回值为 null或配合?:做空判断处理</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token comment">//类型后面加?表示可为空</span>
<span class="token keyword">var</span> age<span class="token operator">:</span> String<span class="token operator">?</span> <span class="token operator">=</span> <span class="token string">"23"</span> 
<span class="token comment">//抛出空指针异常</span>
<span class="token keyword">val</span> ages <span class="token operator">=</span> age<span class="token operator">!!</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">//不做处理返回 null</span>
<span class="token keyword">val</span> ages1 <span class="token operator">=</span> age<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">//age为空返回-1</span>
<span class="token keyword">val</span> ages2 <span class="token operator">=</span> age<span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?:</span> <span class="token operator">-</span><span class="token number">1</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="类型检测及自动转换"><a href="#类型检测及自动转换" class="headerlink" title="类型检测及自动转换"></a>类型检测及自动转换</h4><pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">fun</span> <span class="token function">getStringLength</span><span class="token punctuation">(</span>obj<span class="token operator">:</span> Any<span class="token punctuation">)</span><span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token punctuation">&#123;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>obj <span class="token operator">!</span><span class="token keyword">is</span> String<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token keyword">null</span>
  <span class="token comment">// 在这个分支中, `obj` 的类型会被自动转换为 `String`</span>
  <span class="token keyword">return</span> obj<span class="token punctuation">.</span>length
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h4><p>Kotlin 的基本数值类型包括 Byte、Short、Int、Long、Float、Double 等</p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">位宽度</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Double</td>
<td align="left">64</td>
<td>2.0</td>
</tr>
<tr>
<td align="left">Float</td>
<td align="left">32</td>
<td>2f</td>
</tr>
<tr>
<td align="left">Long</td>
<td align="left">64</td>
<td>3L</td>
</tr>
<tr>
<td align="left">Int</td>
<td align="left">32</td>
<td>3</td>
</tr>
<tr>
<td align="left">Short</td>
<td align="left">16</td>
<td>4</td>
</tr>
<tr>
<td align="left">Byte</td>
<td align="left">8</td>
<td>5</td>
</tr>
</tbody></table>
<p>可以使用下划线使数字常量更易读</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">val</span> oneMillion <span class="token operator">=</span> <span class="token number">1_000_000</span>
<span class="token keyword">val</span> hexBytes <span class="token operator">=</span> <span class="token number">0xFF_EC_DE_5E</span>
<span class="token keyword">val</span> phoneNumber <span class="token operator">=</span> <span class="token number">185_5162_3845</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>在 Kotlin 中，三个等号 === 表示比较对象地址，两个 == 表示比较两个值大小。</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">val</span> a<span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">10000</span>
<span class="token comment">//经过了装箱，创建了两个不同的对象</span>
<span class="token keyword">val</span> boxedA<span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token operator">=</span> a
<span class="token keyword">val</span> anotherBoxedA<span class="token operator">:</span> Int<span class="token operator">?</span> <span class="token operator">=</span> a

<span class="token comment">//虽然经过了装箱，但是值是相等的</span>
<span class="token function">println</span><span class="token punctuation">(</span>boxedA <span class="token operator">===</span> anotherBoxedA<span class="token punctuation">)</span> <span class="token comment">//  false，值相等，对象地址不一样</span>
<span class="token function">println</span><span class="token punctuation">(</span>boxedA <span class="token operator">==</span> anotherBoxedA<span class="token punctuation">)</span> <span class="token comment">// true，值相等</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>类型转换:每种数据类型都有下面的这些方法，可以转化为其它的类型：</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token function">toByte</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Byte
<span class="token function">toShort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Short
<span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Int
<span class="token function">toLong</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Long
<span class="token function">toFloat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Float
<span class="token function">toDouble</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Double
<span class="token function">toChar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> Char<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<p>从kotlin1.3开始，引入了<strong>无符号整型</strong>数据类型，目前还是<em><strong>实验性</strong></em>的。</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>位宽度</th>
<th>范围</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>UByte</td>
<td>8</td>
<td>0~255</td>
<td>221u</td>
</tr>
<tr>
<td>UShort</td>
<td>16</td>
<td>0~2^16^-1</td>
<td>12u</td>
</tr>
<tr>
<td>UInt</td>
<td>32</td>
<td>0~2^32^-1</td>
<td>36u</td>
</tr>
<tr>
<td>ULong</td>
<td>64</td>
<td>0~2^64^-1</td>
<td>1284uL</td>
</tr>
</tbody></table>
<h4 id="kotlin修饰符及关键字"><a href="#kotlin修饰符及关键字" class="headerlink" title="kotlin修饰符及关键字"></a>kotlin修饰符及关键字</h4><pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">public</span> <span class="token operator">/</span> <span class="token keyword">protected</span> <span class="token operator">/</span> <span class="token keyword">private</span> <span class="token operator">/</span> <span class="token keyword">internal</span>
<span class="token keyword">expect</span> <span class="token operator">/</span> <span class="token keyword">actual</span>
<span class="token keyword">final</span> <span class="token operator">/</span> <span class="token keyword">open</span> <span class="token operator">/</span> <span class="token keyword">abstract</span> <span class="token operator">/</span> <span class="token keyword">sealed</span> <span class="token operator">/</span> <span class="token keyword">const</span>
<span class="token keyword">external</span>
<span class="token keyword">override</span>
<span class="token keyword">lateinit</span>
<span class="token keyword">tailrec</span>
<span class="token keyword">vararg</span>
<span class="token keyword">suspend</span>
<span class="token keyword">inner</span>
<span class="token keyword">enum</span> <span class="token operator">/</span> <span class="token keyword">annotation</span>
<span class="token keyword">companion</span>
<span class="token keyword">inline</span>
<span class="token keyword">infix</span>
<span class="token keyword">operator</span>
<span class="token keyword">data</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>与Java不同的是，kotlin默认的访问修饰符是public,而且多了一个internal（模块）修饰符<br>其他private 和 protected的作用范围和Java一样</p>
<ul>
<li>private：只该类可见（包括该类的所有成员）</li>
<li>protected：同private 加子类可见</li>
<li>internal：在同一个模块中可见</li>
<li>public：公共，所有都可见</li>
</ul>
<p>在kotlin中增加了top-level属性和方法，与class同级</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin"><span class="token keyword">package</span> foo<span class="token punctuation">.</span>bar

<span class="token keyword">val</span> prop<span class="token operator">:</span> String <span class="token operator">=</span> <span class="token string">"top-level-prop"</span>
<span class="token keyword">fun</span> <span class="token function">demo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token function">loge</span><span class="token punctuation">(</span><span class="token string">"top-level"</span><span class="token punctuation">,</span> <span class="token string">"top-level-demo()"</span><span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">class</span> Kot <span class="token punctuation">&#123;</span>
    <span class="token keyword">fun</span> <span class="token function">v</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token function">loge</span><span class="token punctuation">(</span><span class="token string">"top-level"</span><span class="token punctuation">,</span> prop<span class="token punctuation">)</span>
        <span class="token function">demo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>上面的prop属性和demo()方法就是顶级属性及方法，在Kot类编译成class文件的时候，会把Top-level的属性和函数创建到以类名+Kt为名的class文件中，如：KotKt.class</p>
<p>顶级属性和方法前面不能用protected访问修饰符</p>
<h4 id="Kotlin随机数"><a href="#Kotlin随机数" class="headerlink" title="Kotlin随机数"></a>Kotlin随机数</h4><p>kotlin1.3加入多平台random随机数，kotlin.random.Random</p>
<pre class="line-numbers language-kotlin" data-language="kotlin"><code class="language-kotlin">Random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//Int.MIN_VALUE ~ Int.MAX_VALUE</span>
Random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">52</span><span class="token punctuation">)</span> <span class="token comment">//0 ~ 52</span>
Random<span class="token punctuation">.</span><span class="token function">nextBoolean</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//true or false</span>
Random<span class="token punctuation">.</span><span class="token function">nextFloat</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//0 ~ 1</span>
Random<span class="token punctuation">.</span><span class="token function">nextDouble</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//0 ~ 1</span>
Random<span class="token punctuation">.</span><span class="token function">nextDouble</span><span class="token punctuation">(</span><span class="token number">5.0</span><span class="token punctuation">)</span> <span class="token comment">//0 ~ 5.0</span>

<span class="token comment">//生成一个长度23的随机数组</span>
<span class="token keyword">val</span> nextBytes <span class="token operator">=</span> Random<span class="token punctuation">.</span><span class="token function">nextBytes</span><span class="token punctuation">(</span><span class="token number">23</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>it <span class="token keyword">in</span> nextBytes<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token function">println</span><span class="token punctuation">(</span>it<span class="token punctuation">)</span>
<span class="token punctuation">&#125;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h4><h5 id="单目运算符"><a href="#单目运算符" class="headerlink" title="单目运算符"></a>单目运算符</h5><table>
<thead>
<tr>
<th>表达式</th>
<th>对应函数</th>
</tr>
</thead>
<tbody><tr>
<td>+a</td>
<td>a.unaryPlus()</td>
</tr>
<tr>
<td>-a</td>
<td>a.unaryMinus()</td>
</tr>
<tr>
<td>in</td>
<td>a.not()</td>
</tr>
<tr>
<td>a++</td>
<td>a.inc()</td>
</tr>
<tr>
<td>a–</td>
<td>a.dec()</td>
</tr>
</tbody></table>
<h5 id="双目运算符"><a href="#双目运算符" class="headerlink" title="双目运算符"></a>双目运算符</h5><table>
<thead>
<tr>
<th>表达式</th>
<th>对应函数</th>
</tr>
</thead>
<tbody><tr>
<td>a+b</td>
<td>a.plus(b)</td>
</tr>
<tr>
<td>a-b</td>
<td>a.minus(b)</td>
</tr>
<tr>
<td>a*b</td>
<td>a.times(b)</td>
</tr>
<tr>
<td>a/b</td>
<td>a.div(b)</td>
</tr>
<tr>
<td>a%b</td>
<td>a.rem(b) / a.mod(b) 已过时</td>
</tr>
<tr>
<td>a..b</td>
<td>a.rangTo(b)</td>
</tr>
<tr>
<td>a in b</td>
<td>a.contains(b)</td>
</tr>
<tr>
<td>a !in b</td>
<td>!a.contains(b)</td>
</tr>
<tr>
<td>a += b</td>
<td>a.plusAssign(b)</td>
</tr>
<tr>
<td>a -= b</td>
<td>a.minusAssign(b)</td>
</tr>
<tr>
<td>a *= b</td>
<td>a.timesAssign(b)</td>
</tr>
<tr>
<td>a /= b</td>
<td>a.divAssign(b)</td>
</tr>
<tr>
<td>a %= b</td>
<td>a.remAssign(b)</td>
</tr>
<tr>
<td>a &amp;&amp; b</td>
<td>a.and(b)</td>
</tr>
<tr>
<td>a || b</td>
<td>a.or(b)</td>
</tr>
</tbody></table>

                
            </div>
            <hr/>

            



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/blog/tags/Kotlin%E8%AF%AD%E6%B3%95/">
                                    <span class="chip bg-color">Kotlin语法</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="wechat,qq,facebook,google,weibo" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/blog/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC81NDQ2OS8zMDk0MA==">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/KotlinString/">
                    <div class="card-image">
                        
                        
                        <img src="/blog/medias/featureimages/32.png" class="responsive-img" alt="Kotlin中String的常用方法">
                        
                        <span class="card-title">Kotlin中String的常用方法</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-09-24
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/blog/categories/Kotlin/" class="post-category">
                                    Kotlin
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/blog/tags/String/">
                        <span class="chip bg-color">String</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/ASKeymapForWindows/">
                    <div class="card-image">
                        
                        
                        <img src="/blog/medias/featureimages/2.jpg" class="responsive-img" alt="快捷键--Android Studio(Windows版)">
                        
                        <span class="card-title">快捷键--Android Studio(Windows版)</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-09-24
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/blog/categories/%E5%AE%9E%E7%94%A8%E5%B7%A5%E5%85%B7/" class="post-category">
                                    实用工具
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/blog/tags/%E5%BF%AB%E6%8D%B7%E9%94%AE/">
                        <span class="chip bg-color">快捷键</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('3'),
            headingSelector: 'h1, h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="2821652960"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2024</span>
            
            <a href="/blog/about" target="_blank">Xyq</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "9";
                        var startDate = "23";
                        var startHour = "12";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/xiayunquan" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:xiayunquan@yeah.net" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=704006530" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 704006530" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/blog/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/blog/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/blog/libs/background/canvas-nest.js"></script>
    

    

    

    
    <script src="/blog/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
